import { Component, OnInit } from '@angular/core';
import {User} from '../user';
import {TestService} from '../test.service';

@Component({
  selector: 'app-test',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {
  currentDate: string;
  user: User = {
    id: 10,
    name: 'my test componet',
    record: true,
  };
  selectedUser: User;
  users: User[];
  constructor(private userService: TestService) { }

  ngOnInit() {
    this.getUsers();
  }
  getUsers(): void {
    this.userService.getUsers().subscribe(users => {
      this.users = users;
    });
  }
  deleteUser(usr): void {
    this.userService.deleteUser(usr.id).subscribe(success => {
      if (success) {
        const index = this.users.findIndex(user => user.id === usr.id);
        this.users.splice(index, 1);
      }
    });
  }
  onSelect(user: User): void {
    this.selectedUser = user;
  }
  detaileLoad(user: User) {
    console.log('detaile has loaded.');
  }
  test(evt: object): void {
    console.log(evt);
    const d = new Date();
    this.currentDate = `${d.getFullYear()}-${d.getMonth().toLocaleString().padStart(2, '0')}`
    + `-${this.paddingTwoChar(d.getDay())} ${this.paddingTwoChar(d.getHours())}`
    + `:${this.paddingTwoChar(d.getMinutes())}:${this.paddingTwoChar(d.getSeconds())}`
    + `,${d.getMilliseconds()}`;
  }
  submitForm(ngForm) {
    console.log(ngForm);
  }
  private paddingTwoChar(value: number, padLen = 2, padChar = '0'): string {
    return value.toLocaleString().padStart(padLen, padChar);
  }
}
